/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.page;

import java.io.Serializable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author: solidwang
 * @date: 17/4/28
 * 分页标签实现
 */
public class PageTag<T> extends TagSupport implements Serializable {

    private static final long serialVersionUID = 1L;

    //是否显示改变页数下拉框
    private boolean skipCount = false;
    //是否显示跳页输入框
    private boolean jumpText = false;
    //是否显示上一页/下一页按钮
    private boolean showPreAndNext = true;
    //是否显示首页/尾页按钮
    private boolean showFirstAndLast = true;
    //是否显示遍历页
    private boolean forPage = true;
    //是否显示当前页和总页数
    private boolean showCurrAndSum = true;
    //查询条件
    private String condition;
    //页面实体
    private Page<T> page;
    //请求url
    private String url;

    @Override
    public int doStartTag() throws JspException {
        try {
            if (page != null) {
                int currentPage = page.getCurrentPage();
                int totalPage = page.getTotalPage();
                StringBuilder builder = new StringBuilder("<script type='text/javascript'>");
                builder.append("function loadPage(pagenum){var url='" + url);
                if (condition == null || "".equals(condition)) {
                    builder.append("?currentPage='+pagenum;self.location.href=url}");
                } else {
                    builder.append("?" + condition + "&currentPage='+pagenum;self.location.href=url}");
                }
                builder.append("function jumpPage(){var jumpPageNum=document.getElementById('jump_page_num').value;");
                builder.append("if(jumpPageNum.length<=0){alert('请填写要跳转的页面值!');return false;}");
                builder.append("if(!(/^[1-9]\\d*$/).test(jumpPageNum)){alert('跳转页面值只能填写正整数');return false;}");
                builder.append("if(jumpPageNum>" + page.getTotalPage() + "){alert('跳转的页数大于总页数!');return false;}");
                builder.append("var url='" + url);
                // 如果分页查询条件不为空，需要加上分页条件。
                if (condition == null || "".equals(condition)) {
                    builder.append("?currentPage='+jumpPageNum;self.location.href=url }");
                } else {
                    builder.append("?" + condition + "&currentPage='+jumpPageNum;self.location.href=url }");
                }
                builder.append("</script>");
                builder.append("<div class='inline pull-right page'>");
                builder.append("共" + page.getTotalCount() + "条记录 " + page.getCurrentPage() + "/" + page.getTotalPage() + "页");

                if (currentPage > 1) {
                    if (showFirstAndLast) {
                        builder.append("<a href='javascript:loadPage(\"1\")'>首页</a>");
                    }
                }

                //google风格的分页
                if (forPage && totalPage > 0) {
                    for (long pagenum = 1; pagenum <= page.getTotalPage(); pagenum++) {
                        if (page.getCurrentPage() < 5 && pagenum < 10) {
                            if (pagenum == page.getCurrentPage()) {
                                builder.append("<span class='current'>" + pagenum + "</span>");
                            } else {
                                builder.append("<a href='" + url);
                                // 如果分页查询条件不为空，需要加上分页条件。
                                if (condition == null || "".equals(condition)) {
                                    builder.append("?currentPage=" + pagenum + "'>" + pagenum + "</a>");
                                } else {
                                    builder.append("?" + condition + "&currentPage=" + pagenum + "'>" + pagenum + "</a>");
                                }
                            }
                        } else {
                            if (pagenum > page.getCurrentPage() - 5 && pagenum < page.getCurrentPage() + 5) {
                                if (pagenum == page.getCurrentPage()) {
                                    builder.append("<span class='current'>" + pagenum + "</span>");
                                } else {
                                    builder.append("<a href='" + url);
                                    // 如果分页查询条件不为空，需要加上分页条件。
                                    if (condition == null || "".equals(condition)) {
                                        builder.append("?currentPage=" + pagenum + "'>" + pagenum + "</a>");
                                    } else {
                                        builder.append("?" + condition + "&currentPage=" + pagenum + "'>" + pagenum + "</a>");
                                    }
                                }
                            }
                        }
                    }
                }
                if (currentPage != totalPage) {
                    if (showFirstAndLast) {
                        builder.append("<a href='javascript:loadPage(\"" + page.getTotalPage() + "\");'>尾页</li>");
                    }
                }
                builder.append("</div>");
                super.pageContext.getOut().print(builder.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }

    public boolean isSkipCount() {
        return skipCount;
    }

    public void setSkipCount(boolean skipCount) {
        this.skipCount = skipCount;
    }

    public boolean isJumpText() {
        return jumpText;
    }

    public void setJumpText(boolean jumpText) {
        this.jumpText = jumpText;
    }

    public boolean isShowPreAndNext() {
        return showPreAndNext;
    }

    public void setShowPreAndNext(boolean showPreAndNext) {
        this.showPreAndNext = showPreAndNext;
    }

    public boolean isShowFirstAndLast() {
        return showFirstAndLast;
    }

    public void setShowFirstAndLast(boolean showFirstAndLast) {
        this.showFirstAndLast = showFirstAndLast;
    }

    public boolean isForPage() {
        return forPage;
    }

    public void setForPage(boolean forPage) {
        this.forPage = forPage;
    }

    public boolean isShowCurrAndSum() {
        return showCurrAndSum;
    }

    public void setShowCurrAndSum(boolean showCurrAndSum) {
        this.showCurrAndSum = showCurrAndSum;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public Page<T> getPage() {
        return page;
    }

    public void setPage(Page<T> page) {
        this.page = page;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
